Repository navigation
Sync binary-search with the updated tutorial - #943
Merged
realpython-bot merged 2 commits intoNov 13, 2026
Merged
Conversation
…Search in Python find_all_indices() tested the leftmost and rightmost indices for truthiness, so a match at index 0 returned an empty set. Compare them against None instead, matching the updated tutorial. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The tutorial and benchmark.py state a Python 3.7 floor, but the parenthesized context managers in download_imdb.py need 3.9. Nested with statements restore the stated floor (vermin: 3.7) without changing behavior.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Companion change for the maintenance update of How to Do a Binary Search in Python.
What changed
search/binary.py:find_all_indices()tested the leftmost and rightmost indices for truthiness, so when the first match sat at index 0 it returned an empty set (find_all_indices(["apple", "apple", "banana"], "apple")returnedset()instead of{0, 1}). It now compares both indices againstNone. The tutorial draft gets the same fix.Links
Verification (Python 3.15.2)
find_all_indices()returns{0, 1},{1, 2}, andset()for a leftmost match at index 0, a middle match, and a miss.find_all()returns{"apple"}.benchmark.pyruns the random, linear, and binary algorithms against a small names file.download_imdb.pydownloads the IMDb dataset and writesnames.txtandsorted_names.txt(15.8M lines each).ruff@0.14.1 format --checkandruff@0.14.1 checkboth pass onbinary-search/.Note:
download_imdb.pyuses parenthesized context managers, so it needs Python 3.9 or later. Thebenchmark.pydocstring and the tutorial both say 3.7. I left both claims unchanged so a human can decide.🤖 Generated with Claude Code